<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_model extends CI_Model {

	public function __construct(){
		parent::__construct();
		
	}

	public function login($user){
		$this->load->database();
		$this->load->library('usuario');

		$usuario = new usuario();
		$usuario = $user;

		$sql = 'SELECT *
					FROM `dw_usuario` `usu` 
						WHERE (`usu`.`usu_username`="'.$usuario->getUsu_username().'" OR `usu`.`usu_email`="'.$usuario->getUsu_username().'") AND `usu`.`usu_password`="'.$usuario->getUsu_password().'" LIMIT 1;';

		$query = $this->db->query( $sql );

		foreach ($query->result_array() as $fila) {
			$usuario->setUsu_id($fila['usu_id']);
			$usuario->setUsu_username($fila['usu_username']);
			$usuario->setUsu_email($fila['usu_email']);
			$usuario->setIsLogged(true);
		}

		return $usuario;
	}

	public function verificarUsuario($usu_username, $usu_email){
		$this->load->database();

		$sql = 'SELECT `usu_username`
					FROM `dw_usuario`
						WHERE `usu_username`="'.$usu_username.'" OR `usu_email`="'.$usu_email.'" LIMIT 1;';

		$query = $this->db->query( $sql );

		if( count($query->result_array()) == 0){
			return false;
		}else{
			return true;
		}
	}

	public function insertarUsuario($user){
		$this->load->database();
		$this->load->library('usuario');

		$usuario = new usuario();
		$usuario = $user;

		$sql = 'INSERT INTO `dw_usuario` (`usu_username`,`usu_password`,`usu_email`)
						VALUES("'.$usuario->getUsu_username().'", "'.$usuario->getUsu_password().'", "'.$usuario->getUsu_email().'");';

		$query = $this->db->query( $sql );
	}

}